Skip to content

Fix cmd_apply branch switch-back handling#21

Open
maxim-ge wants to merge 2 commits intomainfrom
pr-apply-switch-back--pr
Open

Fix cmd_apply branch switch-back handling#21
maxim-ge wants to merge 2 commits intomainfrom
pr-apply-switch-back--pr

Conversation

@maxim-ge
Copy link
Member

Fix cmd_apply branch switch-back handling

Remove premature switch-back from cmd_ffdefault in pr.sh so it stays on the default branch after fast-forwarding. Move prev_branch capture in conf.sh cmd_apply to before the ffdefault call. Add ERR trap to restore the original branch on failure.

@gemini-code-assist
Copy link

Summary of Changes

Hello @maxim-ge, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the branch handling logic within the cmd_apply and cmd_ffdefault scripts to ensure correct and robust branch switching and restoration. The changes centralize the responsibility for returning to the original branch, making the fast-forward operation more atomic and adding error recovery for branch state.

Highlights

  • Refactored Branch Switch-Back Logic: The responsibility for switching back to the original branch after a fast-forward operation has been moved from pr.sh's cmd_ffdefault function to conf.sh's cmd_apply function. cmd_ffdefault now acts as a clean primitive, staying on the default branch after fast-forwarding.
  • Improved Branch Capture in cmd_apply: The prev_branch variable in conf.sh cmd_apply is now captured earlier, before the ffdefault call, ensuring the correct original branch is remembered regardless of subsequent operations.
  • Added Error Handling for Branch Restoration: An ERR trap has been introduced in conf.sh cmd_apply to automatically restore the original branch ($prev_branch) in case of any failure during the fast-forward or subsequent operations, enhancing robustness.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • uspecs/changes/2602251153-pr-apply-switch-back/change.md
    • Added a new change request document explaining the need for cmd_apply to manage branch switch-back.
  • uspecs/changes/2602251153-pr-apply-switch-back/how.md
    • Added a new document detailing the approach to refactor branch handling in pr.sh and conf.sh.
  • uspecs/changes/2602251153-pr-apply-switch-back/impl.md
    • Added a new document outlining the implementation plan for the branch switch-back fix, including specific file updates.
  • uspecs/u/scripts/_lib/pr.sh
    • Removed the premature branch switch-back logic and associated trap from cmd_ffdefault.
  • uspecs/u/scripts/conf.sh
    • Moved the prev_branch capture to occur before the ffdefault call in cmd_apply.
    • Added an ERR trap to cmd_apply to restore the original branch on failure.
    • Removed redundant prev_branch capture from the prbranch section.
    • Cleared the ERR trap after successful pr.sh pr execution.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

…ges/archive/2602/2602251222-pr-apply-switch-back
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the branch switching logic in cmd_apply to make it more robust. The cmd_ffdefault function is simplified to no longer switch back to the original branch, delegating that responsibility to the caller. In cmd_apply, this is handled by capturing the original branch, using an ERR trap for cleanup on failure, and passing the original branch to the pr command for the final switch-back on success. The changes are logical and improve the script's behavior. I've found one issue with the implementation of the ERR trap regarding variable expansion, which would prevent it from working as intended.

if [[ "$pr_flag" == "true" ]]; then
prev_branch=$(cd "$project_dir" && git symbolic-ref --short HEAD)
(cd "$project_dir" && bash "$script_dir/_lib/pr.sh" ffdefault)
trap 'git -C "$project_dir" checkout "$prev_branch" 2>/dev/null || true' ERR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The variables $project_dir and $prev_branch are inside single quotes in the trap command. This prevents them from being expanded when the trap is defined. As a result, the trap would attempt to execute a command with literal, unexpanded variable names (e.g., git -C "$project_dir"), which will fail.

To ensure the variables are correctly expanded with their values when the trap is defined, you should use double quotes for the outer string and escape the inner double quotes.

Suggested change
trap 'git -C "$project_dir" checkout "$prev_branch" 2>/dev/null || true' ERR
trap "git -C \"$project_dir\" checkout \"$prev_branch\" 2>/dev/null || true" ERR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant